package com.learn.spring;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * @author zhongshitao
 * @date 2020/9/24 10:15 上午
 */
public class JDKProxy implements InvocationHandler {

    private Object target;

    public JDKProxy(Object target) {
        this.target = target;
    }

    @Override
    public Object invoke(Object o, Method method, Object[] objects) throws Throwable {
        sayHi();
        Object result = method.invoke(target,objects);
        sayBye();
        return result;
    }

    private void sayHi() {
        System.out.println("say hi");
    }

    private void sayBye() {
        System.out.println("say Bye");
    }


    public <T> T getProxy() {
        T t = (T) Proxy.newProxyInstance(target.getClass().getClassLoader(),
                target.getClass().getInterfaces(), this);
        return t;
    }

}
